package pers.hl.module_main.mine

import android.text.InputFilter
import android.text.InputType
import android.widget.EditText
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.android.synthetic.main.item_rv_common_edit.view.*
import pers.hl.common.ui.CommonTextWatcher
import pers.hl.common.utils.MyUtils
import pers.hl.lib_global.model.bean.User
import pers.hl.module_main.CommonEditActivity
import pers.hl.module_main.R
import pers.hl.module_main.common.BaseLibQuickAdapter
import pers.hl.module_main.common.CommonDialog

class UserInfoAdapter(private val mUser: User, private val onUserUpdateListener: (User) -> Unit) :
    BaseLibQuickAdapter<CommonEditActivity.EditBean, BaseViewHolder>(R.layout.item_rv_common_edit) {

    override fun convert(holder: BaseViewHolder, item: CommonEditActivity.EditBean) {
        holder.setText(R.id.tvTitle, item.title)
        holder.setText(R.id.etContent, item.content)
        holder.setEnabled(R.id.etContent, item.enable)

        dealSpecialLogic(holder, item)
        holder.itemView.etContent.apply {
            setOnClickListener {
                when (item.tagId) {
                    5 -> {
                        showGenderDialog()
                    }
                }
            }
        }
    }

    private fun dealSpecialLogic(holder: BaseViewHolder, item: CommonEditActivity.EditBean) {
        holder.itemView.apply {
            when (item.tagId) {
                4 -> {
                    etContent.inputType = InputType.TYPE_CLASS_NUMBER
                    etContent.filters = arrayOf(InputFilter.LengthFilter(3))
                }
                5 -> etContent.isFocusableInTouchMode = false
                7 -> {
                    etContent.inputType = InputType.TYPE_CLASS_NUMBER
                    etContent.filters = arrayOf(InputFilter.LengthFilter(11))
                }
            }
            val watcher = object : CommonTextWatcher(etContent) {
                override fun textChanged(view: EditText, text: String) {
                    when (item.tagId) {
                        2 -> mUser.userName = text
                        4 -> mUser.useAge = MyUtils.tryParseInt(text)
                        6 -> mUser.userAddr = text
                        7 -> mUser.userTel = text
                    }
                    onUserUpdateListener(mUser)
                }
            }

            // 在onBindViewHolder()中通过在适当的时机添加或移除Edittext的TextChangedListener来处理数据错乱的问题。
            // 这个适当的时机就是选在Edittext获得焦点的时候添加监听器，失去焦点的时候再移除监听器，这样可以保证数据的正确性。
            etContent.setOnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    etContent.addTextChangedListener(watcher)
                } else {
                    etContent.removeTextChangedListener(watcher)
                }
            }
        }
    }

    private fun showGenderDialog() {
        CommonDialog.createBottomList(context, mutableListOf("男", "女"), convertBlock = { holder, item ->
                holder.setText(R.id.tvItem, item)
            },
            onItemClick = { layer, adapter, position, item ->
                layer.dismiss()
                if (position == 0) {
                    mUser.userGender = 1
                }
                if (position == 1) {
                    mUser.userGender = 2
                }
                onUserUpdateListener(mUser)
            }).show()
    }

}